package org.server.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import org.pojo.User;
import org.pojo.UserMeta;
import org.server.tools.DateProcess;
import org.server.util.*;
public class UserDao implements IUserDao{

	private Connection dbcon = null;
	private PreparedStatement pstmt= null;
	public UserDao()
	{
		try {
			dbcon = DBUtil.getConnection();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	public void saveUser(User u) throws SQLException
	{
		boolean autoCommit = dbcon.getAutoCommit();
		String sql = "insert into users values(?,?,?,?,?,?,null,?,?,null)";	
		dbcon.setAutoCommit(false);
		pstmt = dbcon.prepareStatement(sql);
		pstmt.setString(1, u.getUsername());
		pstmt.setString(2,u.getNickname());
		pstmt.setString(3, u.getPassword());
		pstmt.setString(4, u.getSex());
		pstmt.setString(5, u.getSign());
		pstmt.setString(6, u.getPhoto());
		pstmt.setString(7, new DateProcess().process());
		pstmt.setString(8, new DateProcess().process());
		pstmt.executeUpdate();
		dbcon.commit();
		dbcon.setAutoCommit(autoCommit);
	}
	@Override
	public User loadUserByUsername(String username) throws Exception {
		// TODO Auto-generated method stub
		String sql = "select * from users where username = ?";
		pstmt = dbcon.prepareStatement(sql);
		pstmt.setString(1, username);
		ResultSet rs = pstmt.executeQuery();
		User loadUser=null;
		if(rs.next())
		{
			loadUser = new User(rs.getString("nickname"),rs.getString("sex"),
					rs.getString("sign"),rs.getString("photo"),rs.getInt("userid"));
		}
		if(loadUser != null)
		{
			return loadUser;
		}
		return null;
	}
	@Override
	public User loadUserById(int id) throws Exception {
		// TODO Auto-generated method stub
		String sql = "select * from users where userid = ?";
		pstmt = dbcon.prepareStatement(sql);
		pstmt.setInt(1, id);
		ResultSet rs = pstmt.executeQuery();
		User loadUser=null;
		if(rs.next())
		{
			loadUser = new User(rs.getString("nickname"),rs.getString("sex"),
					rs.getString("sign"),rs.getString("photo"),rs.getInt("userid"));
		}
		if(loadUser != null)
		{
			return loadUser;
		}
		return null;
	}
	@Override
	public User loadFriendsByUserame(String username) throws Exception {
		// TODO Auto-generated method stub
		String sql = "select * from users where username = ?";
		pstmt = dbcon.prepareStatement(sql);
		pstmt.setString(1, username);
		ResultSet rs = pstmt.executeQuery();
		User loadUser = new User(rs.getString("username"),rs.getString("password"));
		if(loadUser != null)
		{
			return loadUser;
		}
		return null;
	}
	@Override
	public void deleteUserById(String userid) throws Exception {
		// TODO Auto-generated method stub
		
	}
	@Override
	public void deleteUserByName(String username) throws Exception {
		// TODO Auto-generated method stub
		
	}
	@Override
	public void userUpdate(User user) throws Exception {
		// TODO Auto-generated method stub
		String sql = "update users set nickname = ?, sex = ?, sign = ? where userid = ?";
		pstmt = dbcon.prepareStatement(sql);
		pstmt.setString(1, user.getNickname());
		pstmt.setString(2, user.getSex());
		pstmt.setString(3, user.getSign());
		pstmt.setInt(4,user.getUserid());
		pstmt.executeUpdate();
		
		
	}
	@Override
	public void updateUserMetaDate(UserMeta um) throws Exception {
		// TODO Auto-generated method stub
		
	}
	@Override
	public User loadUserByName(String nama) throws Exception {
		// TODO Auto-generated method stub
		return null;
	}
	@Override
	public String getPasswordByUsername(String username) throws Exception {
		// TODO Auto-generated method stub
		String sql = "select * from users where username = ?";
		pstmt = dbcon.prepareStatement(sql);
		pstmt.setString(1, username);
		ResultSet rs = pstmt.executeQuery();
		String passwd=null;
		if(rs.next())
		{
			passwd = rs.getString("password");
		}
		if(passwd != null)
		{
			return passwd;
		}
		return null;
	}
	@Override
	public boolean isExistUsername(String username) throws Exception {
		// TODO Auto-generated method stub
		String sql = "select * from users where username = ?";
		pstmt = dbcon.prepareStatement(sql);
		pstmt.setString(1, username);
		ResultSet rs = pstmt.executeQuery();
		String name=null;
		if(rs.next())
		{
			name = rs.getString("username");
		}
		if(name != null)
		{
			return true;
		}
		return false;
	}
	@Override
	public void updatePassword(User user) throws Exception {
		// TODO Auto-generated method stub
		String sql = "update users set password = ? where username = ?";
		pstmt = dbcon.prepareStatement(sql);
		pstmt.setString(1, user.getPassword());
		pstmt.setString(2,user.getUsername());
		pstmt.executeUpdate();
		
	}
	
}
